<?php

/**
 * Created by PhpStorm
 * User: 林振宇
 * Date: 2020/8/12
 * Time: 17:42
 */

namespace app\api\service;

use app\api\controller\Base;
use app\api\model\Product as ProductModel;
use app\api\model\VesselType as VesselTypeModel;

class IndexService extends Base
{

    /**
     * 首页商品列表
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: Lin Zhengyu
     * @since: 2020/8/13
     */
    public function getProduct()
    {
        return (new VesselTypeModel())->with(['Product'])->select();
    }

    /**
     * 商品详情
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @since: 2020/8/14
     * @author: Lin Zhengyu
     */
    public function productDetails($id)
    {
        $where['id'] = $id;
        $where['is_show'] = 0;
        return (new ProductModel())->with(['VesselType'])->where($where)->find();
    }

    /**
     * 船只标签
     * @return VesselTypeModel[]|false
     * @throws \think\exception\DbException
     * @author: Lin Zhengyu
     * @since: 2020/8/13
     */
    public function getVesselType()
    {
        return VesselTypeModel::all();
    }

    /**
     * 船源列表
     * @param $id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @since: 2020/8/13
     * @author: Lin Zhengyu
     */
    public function getProductById($id)
    {
        $where['is_show'] = 0;
        $search_text = request()->param("search_text", ''); // 搜索名称
        $where['name'] = array('like', '%' . $search_text . '%');
        if ($id == 0) {
            return (new ProductModel())->with(['VesselType'])->where($where)->field('id,name,image,number,price,min_time,deposit,vessel_type_id')->select();
        }
        $where['vessel_type_id'] = $id;
        return (new ProductModel())->with(['VesselType'])->where($where)->field('id,name,image,number,price,min_time,deposit,vessel_type_id')->select();
    }
}
